package com.base.sweb;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpActivity;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.event.WebTestEvent;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.sweb.js.AndroidInterface;
import com.base.sweb.utils.NotificationsUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/9/16.
 */
@Route(path = Constance.ACTIVITY_URL_COMMON_ACTIVITY)
public class CommonActivity extends CBaseMvpActivity {
    @Autowired
    String url;
    @Autowired
    int key;//0 文件预览 1 打开网页
    @Autowired
    String name;
    @Autowired
    String title;
    private FragmentManager mFragmentManager;
    private SuperWebX5Fragment mSuperWebX5Fragment;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_common;
    }

    @Override
    protected void init() {
        mFragmentManager = this.getSupportFragmentManager();
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
    }

    @Override
    protected void start() {
        openFragment();
    }


    private void openFragment() {

        FragmentTransaction ft = mFragmentManager.beginTransaction();
        Bundle mBundle = null;

        switch (key) {
            //文件浏览功能(pdf/ppt/doc/excel/txt)
            case 0:
                ARouter.getInstance().build(Constance.ACTIVITY_URL_FILEDISPLAY_ACTIVITY)
                        .withString("name",name)
                        .withString("title",title)
                        .withString("url",url)
                        .navigation();
                finish();
                break;
            //文件上传(Android与JS通信)
            case 1:
                ft.add(R.id.container_framelayout, mSuperWebX5Fragment = SuperWebX5Fragment.getInstance(mBundle = new Bundle()), SuperWebX5Fragment.class.getName());
//                mBundle.putString(SuperWebX5Fragment.URL_KEY, "file:///android_asset/upload_file/jsuploadfile.html");
                mBundle.putString(SuperWebX5Fragment.URL_KEY,url);
                break;
            //文件下载功能
            case 2:
                checkNotification();
                ft.add(R.id.container_framelayout, mSuperWebX5Fragment = SuperWebX5Fragment.getInstance(mBundle = new Bundle()), SuperWebX5Fragment.class.getName());
//                mBundle.putString(SuperWebX5Fragment.URL_KEY, "http://sj.qq.com/myapp/");
                mBundle.putString(SuperWebX5Fragment.URL_KEY, url);
                break;
            //Android与JS交互
            case 3:
//                ft.add(R.id.container_framelayout, mSuperWebX5Fragment = JsFragment.getInstance(mBundle = new Bundle()), SuperWebX5Fragment.class.getName());
//                mBundle.putString(SuperWebX5Fragment.URL_KEY, "file:///android_asset/js_interaction/hello.html");
//                mBundle.putString(SuperWebX5Fragment.URL_KEY, url);
                break;
            //自定义交互(进度条、下拉回弹)
            case 4:
                ft.add(R.id.container_framelayout, mSuperWebX5Fragment = SlidingFragment.getInstance(mBundle = new Bundle()), SlidingFragment.class.getName());
                mBundle.putString(SuperWebX5Fragment.URL_KEY, "http://m.jd.com/");
                mBundle.putString(SuperWebX5Fragment.URL_KEY, url);
                break;
            //视频播放
            case 5:
//                VideoPlayActivity.show(CommonActivity.this,"http://baobab.kaiyanapp.com/api/v1/playUrl?vid=120943&resourceType=video&editionType=default&source=aliyun");
//                VideoPlayActivity.show(CommonActivity.this,url);
                finish();
                break;
            case 6:
                ARouter.getInstance().build(Constance.ACTIVITY_URL_FILEDISPLAY_ACTIVITY)
                        .withString("name",name)
                        .withString("title",title)
                        .withString("url",url)
                        .withBoolean("isPdf",true)
                        .navigation();
                finish();
                break;
            default:
                break;
        }
        ft.commit();

    }
    private void checkNotification() {
        if (!NotificationsUtils.isNotificationEnabled(this)) {
            new AlertDialog.Builder(this)
                    .setMessage("检测到您没有打开通知权限，是否去打开?")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            Intent localIntent = new Intent();
                            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                            localIntent.setData(Uri.fromParts("package", CommonActivity.this.getPackageName(), null));
                            startActivity(localIntent);
                        }
                    })
                    .create().show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mSuperWebX5Fragment.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        SuperWebX5Fragment mAgentWebX5Fragment = this.mSuperWebX5Fragment;
        if (mAgentWebX5Fragment != null) {
            FragmentKeyDown mFragmentKeyDown = mAgentWebX5Fragment;
            if (mFragmentKeyDown.onFragmentKeyDown(keyCode, event)) {
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
